﻿namespace XServer.Models.DrawCards
{
    public class CardPool
    {
        public readonly CardPoolType cardPoolType;  // 卡池类型
        public readonly int safePity;   // 歪卡池保底次数
        public readonly CardPoolConfig config;

        public CardPool(CardPoolConfig config)
        {
            this.config = config;
            this.cardPoolType = config.cardPoolType;
            this.safePity = config.safePity;

            hardPityRemaining = config.fiveStarPityPulls;
        }

        /// <summary>
        /// 累计抽取总次数
        /// </summary>
        public int totalPulls { get; set; }

        /// <summary>
        /// 未抽出五星累计抽取次数
        /// </summary>
        public int pityAccumulator { get; set; }

        /// <summary>
        /// 保底剩余抽取次数
        /// </summary>
        public int hardPityRemaining { get; set; }

        /// <summary>
        /// 保底状态
        /// </summary>
        public PityState isPityActive { get; set; }

        /// <summary>
        /// 歪卡池次数
        /// </summary>
        public int crookedPity { get; set; }

        /// <summary>
        /// 未抽到四星累计抽取次数
        /// </summary>
        public int fourStarPity { get; set; }

        /// <summary>
        /// UP五星累计抽取数量
        /// </summary>
        public int fiveStarUpCount { get; set; }

        /// <summary>
        /// 常驻五星累计抽取数量
        /// </summary>
        public int recidentCount { get; set; }

        /// <summary>
        /// 五星累计抽取数量
        /// </summary>
        public int fiveStarCount { get; set; }

        /// <summary>
        /// 四星累计抽取数量
        /// </summary>
        public int fourStarCount { get; set; }

        /// <summary>
        /// 抽到五星
        /// </summary>
        /// <param name="isup"></param>
        public void GetFiveStar(bool isup)
        {
            totalPulls++;
            fiveStarCount++;

            if (isup)
            {
                // 限定五星，重置保底状态
                crookedPity = 0;
                isPityActive = PityState.Normal;
                fiveStarUpCount++;
            }
            else
            {
                recidentCount++;
                crookedPity++;
                isPityActive = crookedPity < safePity ? PityState.SmallPity : PityState.BigPity;    // 如果歪卡池数量未达到最大歪卡池数，则保底状态设为小保底
            }

            pityAccumulator = 0;
            hardPityRemaining = config.fiveStarPityPulls;
        }

        /// <summary>
        /// 抽到四星
        /// </summary>
        /// <param name="isup"></param>
        public void GetFourStar()
        {
            totalPulls++;
            fourStarCount++;
            fourStarPity = 0;

            hardPityRemaining--;
            pityAccumulator++;
        }

        /// <summary>
        /// 抽到三星
        /// </summary>
        public void GetThreeStar()
        {
            totalPulls++;
            hardPityRemaining--;
            pityAccumulator++;
            fourStarPity++;
        }
    }

    public class CardPoolConfig
    {
        /// <summary>
        /// 卡池类型
        /// </summary>
        public CardPoolType cardPoolType { get; set; }

        /// <summary>
        /// 歪卡池保底次数
        /// </summary>
        public int safePity { get; set; }

        /// <summary>
        /// 五星保底抽数
        /// </summary>
        public int fiveStarPityPulls { get; set; }

        /// <summary>
        /// 五星抽卡基础概率
        /// </summary>
        public double fiveStarProbability { get; set; }

        /// <summary>
        /// 五星抽卡增长概率
        /// </summary>
        public double fiveStarAddProbability { get; set; }

        /// <summary>
        /// 五星抽卡开始增长抽数
        /// </summary>
        public int fiveStarAddPulls { get; set; }

        /// <summary>
        /// 四星保底抽数
        /// </summary>
        public int fourStarPityPulls { get; set; }

        /// <summary>
        /// 四星抽卡基础概率
        /// </summary>
        public double fourStarProbability { get; set; }

        /// <summary>
        /// 四星抽卡增长概率
        /// </summary>
        public double fourStarAddProbability { get; set; }

        /// <summary>
        /// 四星抽卡开始增长抽数
        /// </summary>
        public int fourStarAddPulls { get; set; }

    }

    /// <summary>
    /// 卡池类型
    /// </summary>
    public enum CardPoolType
    {
        UP,         // 限定
        Recident    // 常驻
    }

    /// <summary>
    /// 保底状态
    /// </summary>
    public enum PityState
    {
        Normal,     // 普通
        SmallPity,  // 小保底
        BigPity,    // 大保底
    }

}
